package com.bjfu.gmms.servlet;

import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.StudentMapper;
import com.bjfu.gmms.model.StudentResult;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/studentQueryServlet")
public class StudentQueryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        int studentId = Integer.parseInt(request.getParameter("studentId"));
        try(SqlSession sqlSession = MyBatisUtil.getSqlSession("development")){
            // 获取Mapper
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

            StudentResult studentResult = studentMapper.getStudentResultById(studentId);
            // 将结果保存到请求作用域
            request.setAttribute("studentResult", studentResult);
            System.out.println(studentResult);
            // 转发请求到 JSP 页面
            request.getRequestDispatcher("/studentResult.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            // 处理异常并返回错误页面
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "查询学生信息失败");
        }

    }
}
